<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2023/11/15
  Time: 13:47
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link type="text/css" href="${pageContext.request.contextPath}/css/login.css" rel="stylesheet">
    <script>

        function login(){
            //1 创建 ajax对象
            let xhr = new XMLHttpRequest();


            //2发起请求
            let method = "GET";
            let username = document.getElementById("username").value;
            let password = document.getElementById("password").value;
            let url = "${pageContext.request.contextPath}/login?username="+username+"&password="+password;
            let async = true;


            xhr.onreadystatechange = function(){

                if( xhr.readyState==4 && xhr.status==200){
                    let result  = eval( "("+xhr.responseText+")" );// result  Object (JSON)
                    if(result.status){
                        alert("登录成功");
                        const taget = document.getElementById("target").value;
                        if(taget){
                            window.location.href = taget;
                        }else{
                            window.location.href = "${pageContext.request.contextPath}/index.jsp";
                        }



                    }else{
                        alert("用户名或者密码不对")
                    }
                }
            }

            xhr.open(method,url,async);
            xhr.send();
        }



    </script>
</head>
<body>

    <div >

        <input type="hidden"  id="target" value="${param["target"]}">

        <form>
                用户名: <input type="text" name="username" id="username" placeholder="请输入用户名" /> <br>
                密 码: <input type="password" name="password" id="password" placeholder="请输入用户名" /> <br>

            <button type="button" onclick="login()">登录</button>

        </form>

    </div>



</body>
</html>
